{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "functional-campus",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import keras as K\n",
    "import tensorflow as tf\n",
    "import gzip\n",
    "import random\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "mexican-michigan",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df =  pd.read_csv('train_1.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "married-samuel",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1018061\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>click</th>\n",
       "      <th>hour</th>\n",
       "      <th>C1</th>\n",
       "      <th>banner_pos</th>\n",
       "      <th>site_id</th>\n",
       "      <th>site_domain</th>\n",
       "      <th>site_category</th>\n",
       "      <th>app_id</th>\n",
       "      <th>app_domain</th>\n",
       "      <th>...</th>\n",
       "      <th>device_type</th>\n",
       "      <th>device_conn_type</th>\n",
       "      <th>C14</th>\n",
       "      <th>C15</th>\n",
       "      <th>C16</th>\n",
       "      <th>C17</th>\n",
       "      <th>C18</th>\n",
       "      <th>C19</th>\n",
       "      <th>C20</th>\n",
       "      <th>C21</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.422380e+19</td>\n",
       "      <td>1</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>f9c69707</td>\n",
       "      <td>e16ceb4b</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>19665</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>2253</td>\n",
       "      <td>2</td>\n",
       "      <td>303</td>\n",
       "      <td>100126</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.422380e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1fbe01fe</td>\n",
       "      <td>f3845767</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>15699</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>1722</td>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>-1</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.422390e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>85f751fd</td>\n",
       "      <td>c4e18dd6</td>\n",
       "      <td>50e219e0</td>\n",
       "      <td>c0c5eb49</td>\n",
       "      <td>f2f777fb</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>21611</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>2480</td>\n",
       "      <td>3</td>\n",
       "      <td>297</td>\n",
       "      <td>100111</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.422400e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>e151e245</td>\n",
       "      <td>7e091613</td>\n",
       "      <td>f028772b</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>20359</td>\n",
       "      <td>216</td>\n",
       "      <td>36</td>\n",
       "      <td>2333</td>\n",
       "      <td>0</td>\n",
       "      <td>39</td>\n",
       "      <td>-1</td>\n",
       "      <td>157</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.422470e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>f282ab5a</td>\n",
       "      <td>61eb5bc4</td>\n",
       "      <td>f028772b</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>18993</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>2161</td>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>-1</td>\n",
       "      <td>157</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             id  click      hour    C1  banner_pos   site_id site_domain  \\\n",
       "0  1.422380e+19      1  14102100  1005           0  f9c69707    e16ceb4b   \n",
       "1  1.422380e+19      0  14102100  1005           0  1fbe01fe    f3845767   \n",
       "2  1.422390e+19      0  14102100  1005           0  85f751fd    c4e18dd6   \n",
       "3  1.422400e+19      0  14102100  1005           1  e151e245    7e091613   \n",
       "4  1.422470e+19      0  14102100  1005           0  f282ab5a    61eb5bc4   \n",
       "\n",
       "  site_category    app_id app_domain  ... device_type device_conn_type    C14  \\\n",
       "0      28905ebd  ecad2386   7801e8d9  ...           1                0  19665   \n",
       "1      28905ebd  ecad2386   7801e8d9  ...           1                0  15699   \n",
       "2      50e219e0  c0c5eb49   f2f777fb  ...           1                0  21611   \n",
       "3      f028772b  ecad2386   7801e8d9  ...           1                0  20359   \n",
       "4      f028772b  ecad2386   7801e8d9  ...           1                0  18993   \n",
       "\n",
       "   C15  C16   C17  C18  C19     C20  C21  \n",
       "0  320   50  2253    2  303  100126   52  \n",
       "1  320   50  1722    0   35      -1   79  \n",
       "2  320   50  2480    3  297  100111   61  \n",
       "3  216   36  2333    0   39      -1  157  \n",
       "4  320   50  2161    0   35      -1  157  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(len(train_df))\n",
    "train_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "concrete-triple",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df =  pd.read_csv('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "intense-spirit",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "500578\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>hour</th>\n",
       "      <th>C1</th>\n",
       "      <th>banner_pos</th>\n",
       "      <th>site_id</th>\n",
       "      <th>site_domain</th>\n",
       "      <th>site_category</th>\n",
       "      <th>app_id</th>\n",
       "      <th>app_domain</th>\n",
       "      <th>app_category</th>\n",
       "      <th>...</th>\n",
       "      <th>device_type</th>\n",
       "      <th>device_conn_type</th>\n",
       "      <th>C14</th>\n",
       "      <th>C15</th>\n",
       "      <th>C16</th>\n",
       "      <th>C17</th>\n",
       "      <th>C18</th>\n",
       "      <th>C19</th>\n",
       "      <th>C20</th>\n",
       "      <th>C21</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.190000e+19</td>\n",
       "      <td>14103105</td>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>178a7b89</td>\n",
       "      <td>b4598159</td>\n",
       "      <td>f028772b</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>17893</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>2039</td>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>100081</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.190000e+19</td>\n",
       "      <td>14103105</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>85f751fd</td>\n",
       "      <td>c4e18dd6</td>\n",
       "      <td>50e219e0</td>\n",
       "      <td>c8e3e3c1</td>\n",
       "      <td>b5f3b24a</td>\n",
       "      <td>8ded1f7a</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>21666</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>2494</td>\n",
       "      <td>3</td>\n",
       "      <td>39</td>\n",
       "      <td>-1</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.190000e+19</td>\n",
       "      <td>14103105</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1fbe01fe</td>\n",
       "      <td>f3845767</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>23141</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>2664</td>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>-1</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.190000e+19</td>\n",
       "      <td>14103105</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>93eaba74</td>\n",
       "      <td>7687a86e</td>\n",
       "      <td>3e814130</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>19016</td>\n",
       "      <td>300</td>\n",
       "      <td>250</td>\n",
       "      <td>2162</td>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>-1</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.190000e+19</td>\n",
       "      <td>14103105</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1fbe01fe</td>\n",
       "      <td>f3845767</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>23141</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>2664</td>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>-1</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             id      hour    C1  banner_pos   site_id site_domain  \\\n",
       "0  1.190000e+19  14103105  1005           1  178a7b89    b4598159   \n",
       "1  1.190000e+19  14103105  1005           0  85f751fd    c4e18dd6   \n",
       "2  1.190000e+19  14103105  1005           0  1fbe01fe    f3845767   \n",
       "3  1.190000e+19  14103105  1005           0  93eaba74    7687a86e   \n",
       "4  1.190000e+19  14103105  1005           0  1fbe01fe    f3845767   \n",
       "\n",
       "  site_category    app_id app_domain app_category  ... device_type  \\\n",
       "0      f028772b  ecad2386   7801e8d9     07d7df22  ...           1   \n",
       "1      50e219e0  c8e3e3c1   b5f3b24a     8ded1f7a  ...           1   \n",
       "2      28905ebd  ecad2386   7801e8d9     07d7df22  ...           1   \n",
       "3      3e814130  ecad2386   7801e8d9     07d7df22  ...           1   \n",
       "4      28905ebd  ecad2386   7801e8d9     07d7df22  ...           1   \n",
       "\n",
       "  device_conn_type    C14  C15  C16   C17  C18  C19     C20  C21  \n",
       "0                0  17893  320   50  2039    2   39  100081   32  \n",
       "1                2  21666  320   50  2494    3   39      -1  112  \n",
       "2                0  23141  320   50  2664    0   35      -1   51  \n",
       "3                0  19016  300  250  2162    2   39      -1   33  \n",
       "4                0  23141  320   50  2664    0   35      -1   51  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(len(test_df))\n",
    "test_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "laden-landscape",
   "metadata": {},
   "outputs": [],
   "source": [
    "submission_df = pd.read_csv('sampleSubmission.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "minute-summary",
   "metadata": {},
   "outputs": [],
   "source": [
    "def to_date_column(df):\n",
    "    df[\"dt_hour\"] = pd.to_datetime(df[\"hour\"], format=\"%y%m%d%H\")\n",
    "    df[\"year\"] = df[\"dt_hour\"].dt.year\n",
    "    df[\"month\"] = df[\"dt_hour\"].dt.month\n",
    "    df[\"day\"] = df[\"dt_hour\"].dt.day\n",
    "    df[\"int_hour\"] = df[\"dt_hour\"].dt.hour\n",
    "    df[\"is_weekday\"] = df[\"dt_hour\"].dt.dayofweek\n",
    "    df[\"is_weekend\"] = df.apply(lambda x: x[\"is_weekday\"] in [5, 6], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "lucky-details",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>click</th>\n",
       "      <th>hour</th>\n",
       "      <th>C1</th>\n",
       "      <th>banner_pos</th>\n",
       "      <th>site_id</th>\n",
       "      <th>site_domain</th>\n",
       "      <th>site_category</th>\n",
       "      <th>app_id</th>\n",
       "      <th>app_domain</th>\n",
       "      <th>...</th>\n",
       "      <th>C19</th>\n",
       "      <th>C20</th>\n",
       "      <th>C21</th>\n",
       "      <th>dt_hour</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "      <th>int_hour</th>\n",
       "      <th>is_weekday</th>\n",
       "      <th>is_weekend</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.422380e+19</td>\n",
       "      <td>1</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>f9c69707</td>\n",
       "      <td>e16ceb4b</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>303</td>\n",
       "      <td>100126</td>\n",
       "      <td>52</td>\n",
       "      <td>2014-10-21</td>\n",
       "      <td>2014</td>\n",
       "      <td>10</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.422380e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1fbe01fe</td>\n",
       "      <td>f3845767</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>35</td>\n",
       "      <td>-1</td>\n",
       "      <td>79</td>\n",
       "      <td>2014-10-21</td>\n",
       "      <td>2014</td>\n",
       "      <td>10</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.422390e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>85f751fd</td>\n",
       "      <td>c4e18dd6</td>\n",
       "      <td>50e219e0</td>\n",
       "      <td>c0c5eb49</td>\n",
       "      <td>f2f777fb</td>\n",
       "      <td>...</td>\n",
       "      <td>297</td>\n",
       "      <td>100111</td>\n",
       "      <td>61</td>\n",
       "      <td>2014-10-21</td>\n",
       "      <td>2014</td>\n",
       "      <td>10</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.422400e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>e151e245</td>\n",
       "      <td>7e091613</td>\n",
       "      <td>f028772b</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>39</td>\n",
       "      <td>-1</td>\n",
       "      <td>157</td>\n",
       "      <td>2014-10-21</td>\n",
       "      <td>2014</td>\n",
       "      <td>10</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.422470e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>14102100</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>f282ab5a</td>\n",
       "      <td>61eb5bc4</td>\n",
       "      <td>f028772b</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>...</td>\n",
       "      <td>35</td>\n",
       "      <td>-1</td>\n",
       "      <td>157</td>\n",
       "      <td>2014-10-21</td>\n",
       "      <td>2014</td>\n",
       "      <td>10</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             id  click      hour    C1  banner_pos   site_id site_domain  \\\n",
       "0  1.422380e+19      1  14102100  1005           0  f9c69707    e16ceb4b   \n",
       "1  1.422380e+19      0  14102100  1005           0  1fbe01fe    f3845767   \n",
       "2  1.422390e+19      0  14102100  1005           0  85f751fd    c4e18dd6   \n",
       "3  1.422400e+19      0  14102100  1005           1  e151e245    7e091613   \n",
       "4  1.422470e+19      0  14102100  1005           0  f282ab5a    61eb5bc4   \n",
       "\n",
       "  site_category    app_id app_domain  ...  C19     C20  C21    dt_hour  year  \\\n",
       "0      28905ebd  ecad2386   7801e8d9  ...  303  100126   52 2014-10-21  2014   \n",
       "1      28905ebd  ecad2386   7801e8d9  ...   35      -1   79 2014-10-21  2014   \n",
       "2      50e219e0  c0c5eb49   f2f777fb  ...  297  100111   61 2014-10-21  2014   \n",
       "3      f028772b  ecad2386   7801e8d9  ...   39      -1  157 2014-10-21  2014   \n",
       "4      f028772b  ecad2386   7801e8d9  ...   35      -1  157 2014-10-21  2014   \n",
       "\n",
       "   month  day  int_hour  is_weekday  is_weekend  \n",
       "0     10   21         0           1       False  \n",
       "1     10   21         0           1       False  \n",
       "2     10   21         0           1       False  \n",
       "3     10   21         0           1       False  \n",
       "4     10   21         0           1       False  \n",
       "\n",
       "[5 rows x 31 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "to_date_column(train_df)\n",
    "train_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "appointed-penalty",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['id', 'click', 'hour', 'C1', 'banner_pos', 'site_id', 'site_domain',\n",
       "       'site_category', 'app_id', 'app_domain', 'app_category', 'device_id',\n",
       "       'device_ip', 'device_model', 'device_type', 'device_conn_type', 'C14',\n",
       "       'C15', 'C16', 'C17', 'C18', 'C19', 'C20', 'C21', 'dt_hour', 'year',\n",
       "       'month', 'day', 'int_hour', 'is_weekday', 'is_weekend'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eastern-economics",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id                  492964\n",
       "click                    2\n",
       "hour                     6\n",
       "C1                       7\n",
       "banner_pos               7\n",
       "site_id               2093\n",
       "site_domain           2044\n",
       "site_category           20\n",
       "app_id                2339\n",
       "app_domain             159\n",
       "app_category            23\n",
       "device_id            85978\n",
       "device_ip           320074\n",
       "device_model          4602\n",
       "device_type              4\n",
       "device_conn_type         4\n",
       "C14                    606\n",
       "C15                      8\n",
       "C16                      9\n",
       "C17                    162\n",
       "C18                      4\n",
       "C19                     41\n",
       "C20                    162\n",
       "C21                     35\n",
       "dt_hour                  6\n",
       "year                     1\n",
       "month                    1\n",
       "day                      1\n",
       "int_hour                 6\n",
       "is_weekday               1\n",
       "is_weekend               1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "lesser-interpretation",
   "metadata": {},
   "outputs": [],
   "source": [
    "label_col = \"click\"\n",
    "x_columns = set(list(train_df.columns)) - set([\"id\", \"site_id\", \"app_id\", \"hour\", \"dt_hour\", \"device_id\", \"device_ip\"] + [label_col])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "atlantic-affair",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train = train_df[x_columns]\n",
    "y_train = train_df[label_col]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "double-renewal",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "C1                     7\n",
       "device_type            4\n",
       "C18                    4\n",
       "month                  1\n",
       "C21                   35\n",
       "device_conn_type       4\n",
       "device_model        4602\n",
       "day                    1\n",
       "site_category         20\n",
       "C20                  162\n",
       "is_weekend             1\n",
       "int_hour               6\n",
       "C15                    8\n",
       "C19                   41\n",
       "app_domain           159\n",
       "year                   1\n",
       "site_domain         2044\n",
       "app_category          23\n",
       "banner_pos             7\n",
       "is_weekday             1\n",
       "C16                    9\n",
       "C17                  162\n",
       "C14                  606\n",
       "dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train.nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "quarterly-freeware",
   "metadata": {},
   "outputs": [],
   "source": [
    "to_date_column(test_df)\n",
    "x_test = test_df[x_columns]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "discrete-broad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C1</th>\n",
       "      <th>device_type</th>\n",
       "      <th>C18</th>\n",
       "      <th>month</th>\n",
       "      <th>C21</th>\n",
       "      <th>device_conn_type</th>\n",
       "      <th>device_model</th>\n",
       "      <th>day</th>\n",
       "      <th>site_category</th>\n",
       "      <th>C20</th>\n",
       "      <th>...</th>\n",
       "      <th>C19</th>\n",
       "      <th>app_domain</th>\n",
       "      <th>year</th>\n",
       "      <th>site_domain</th>\n",
       "      <th>app_category</th>\n",
       "      <th>banner_pos</th>\n",
       "      <th>is_weekday</th>\n",
       "      <th>C16</th>\n",
       "      <th>C17</th>\n",
       "      <th>C14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>76dc4769</td>\n",
       "      <td>31</td>\n",
       "      <td>f028772b</td>\n",
       "      <td>100081</td>\n",
       "      <td>...</td>\n",
       "      <td>39</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>2014</td>\n",
       "      <td>b4598159</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>2039</td>\n",
       "      <td>17893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>10</td>\n",
       "      <td>112</td>\n",
       "      <td>2</td>\n",
       "      <td>2c5339cd</td>\n",
       "      <td>31</td>\n",
       "      <td>50e219e0</td>\n",
       "      <td>-1</td>\n",
       "      <td>...</td>\n",
       "      <td>39</td>\n",
       "      <td>b5f3b24a</td>\n",
       "      <td>2014</td>\n",
       "      <td>c4e18dd6</td>\n",
       "      <td>8ded1f7a</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>2494</td>\n",
       "      <td>21666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>51</td>\n",
       "      <td>0</td>\n",
       "      <td>c6263d8a</td>\n",
       "      <td>31</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>-1</td>\n",
       "      <td>...</td>\n",
       "      <td>35</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>2014</td>\n",
       "      <td>f3845767</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>2664</td>\n",
       "      <td>23141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>33</td>\n",
       "      <td>0</td>\n",
       "      <td>1f0bc64f</td>\n",
       "      <td>31</td>\n",
       "      <td>3e814130</td>\n",
       "      <td>-1</td>\n",
       "      <td>...</td>\n",
       "      <td>39</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>2014</td>\n",
       "      <td>7687a86e</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>250</td>\n",
       "      <td>2162</td>\n",
       "      <td>19016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1005</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>51</td>\n",
       "      <td>0</td>\n",
       "      <td>c6263d8a</td>\n",
       "      <td>31</td>\n",
       "      <td>28905ebd</td>\n",
       "      <td>-1</td>\n",
       "      <td>...</td>\n",
       "      <td>35</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>2014</td>\n",
       "      <td>f3845767</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>2664</td>\n",
       "      <td>23141</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     C1  device_type  C18  month  C21  device_conn_type device_model  day  \\\n",
       "0  1005            1    2     10   32                 0     76dc4769   31   \n",
       "1  1005            1    3     10  112                 2     2c5339cd   31   \n",
       "2  1005            1    0     10   51                 0     c6263d8a   31   \n",
       "3  1005            1    2     10   33                 0     1f0bc64f   31   \n",
       "4  1005            1    0     10   51                 0     c6263d8a   31   \n",
       "\n",
       "  site_category     C20  ...  C19  app_domain  year  site_domain app_category  \\\n",
       "0      f028772b  100081  ...   39    7801e8d9  2014     b4598159     07d7df22   \n",
       "1      50e219e0      -1  ...   39    b5f3b24a  2014     c4e18dd6     8ded1f7a   \n",
       "2      28905ebd      -1  ...   35    7801e8d9  2014     f3845767     07d7df22   \n",
       "3      3e814130      -1  ...   39    7801e8d9  2014     7687a86e     07d7df22   \n",
       "4      28905ebd      -1  ...   35    7801e8d9  2014     f3845767     07d7df22   \n",
       "\n",
       "   banner_pos is_weekday  C16   C17    C14  \n",
       "0           1          4   50  2039  17893  \n",
       "1           0          4   50  2494  21666  \n",
       "2           0          4   50  2664  23141  \n",
       "3           0          4  250  2162  19016  \n",
       "4           0          4   50  2664  23141  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "concrete-chicago",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "\n",
    "\n",
    "x_train_len = len(x_train)\n",
    "n_df = pd.concat([x_train, x_test])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "completed-layer",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C1</th>\n",
       "      <th>device_type</th>\n",
       "      <th>C18</th>\n",
       "      <th>month</th>\n",
       "      <th>C21</th>\n",
       "      <th>device_conn_type</th>\n",
       "      <th>device_model</th>\n",
       "      <th>day</th>\n",
       "      <th>site_category</th>\n",
       "      <th>C20</th>\n",
       "      <th>...</th>\n",
       "      <th>C19</th>\n",
       "      <th>app_domain</th>\n",
       "      <th>year</th>\n",
       "      <th>site_domain</th>\n",
       "      <th>app_category</th>\n",
       "      <th>banner_pos</th>\n",
       "      <th>is_weekday</th>\n",
       "      <th>C16</th>\n",
       "      <th>C17</th>\n",
       "      <th>C14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>2468</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>92</td>\n",
       "      <td>...</td>\n",
       "      <td>18</td>\n",
       "      <td>85</td>\n",
       "      <td>0</td>\n",
       "      <td>2235</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>101</td>\n",
       "      <td>377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>0</td>\n",
       "      <td>2178</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>0</td>\n",
       "      <td>2393</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>45</td>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>0</td>\n",
       "      <td>966</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>82</td>\n",
       "      <td>...</td>\n",
       "      <td>16</td>\n",
       "      <td>171</td>\n",
       "      <td>0</td>\n",
       "      <td>1948</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>161</td>\n",
       "      <td>623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>2575</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>85</td>\n",
       "      <td>0</td>\n",
       "      <td>1238</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>127</td>\n",
       "      <td>476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>581</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>0</td>\n",
       "      <td>956</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>90</td>\n",
       "      <td>343</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   C1  device_type  C18  month  C21  device_conn_type  device_model  day  \\\n",
       "0   2            1    2      0   13                 0          2468    0   \n",
       "1   2            1    0      0   19                 0          2178    0   \n",
       "2   2            1    3      0   14                 0           966    0   \n",
       "3   2            1    0      0   35                 0          2575    0   \n",
       "4   2            1    0      0   35                 0           581    0   \n",
       "\n",
       "   site_category  C20  ...  C19  app_domain  year  site_domain  app_category  \\\n",
       "0              1   92  ...   18          85     0         2235             0   \n",
       "1              1    0  ...    1          85     0         2393             0   \n",
       "2              5   82  ...   16         171     0         1948             3   \n",
       "3             18    0  ...    2          85     0         1238             0   \n",
       "4             18    0  ...    1          85     0          956             0   \n",
       "\n",
       "   banner_pos  is_weekday  C16  C17  C14  \n",
       "0           0           0    2  101  377  \n",
       "1           0           0    2   45  206  \n",
       "2           0           0    2  161  623  \n",
       "3           1           0    1  127  476  \n",
       "4           0           0    2   90  343  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = defaultdict(LabelEncoder)\n",
    "n_df = n_df.apply(lambda x: d[x.name].fit_transform(x))\n",
    "n_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "dutch-equity",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1518639"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "\n",
    "scaler = StandardScaler()\n",
    "scaler.fit(n_df)\n",
    "n_df = scaler.transform(n_df)\n",
    "len(n_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "secure-dallas",
   "metadata": {},
   "outputs": [],
   "source": [
    "n_x_train = n_df[:x_train_len]\n",
    "n_x_test = n_df[x_train_len:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "coordinated-retention",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.\n",
      "[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    7.7s finished\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "\n",
    "assert len(n_x_train) == len(y_train)\n",
    "clf = LogisticRegression(verbose=True)\n",
    "clf.fit(n_x_train, y_train)\n",
    "\n",
    "\n",
    "y_train_star = clf.predict(n_x_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "optical-crest",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.523227653985857"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import log_loss\n",
    "\n",
    "\n",
    "log_loss(y_train, y_train_star)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "labeled-concentration",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8400862030860626"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_curve\n",
    "\n",
    "accuracy_score(y_train, y_train_star)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "superior-point",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[854933,    993],\n",
       "       [161809,    326]], dtype=int64)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "confusion_matrix(y_train, y_train_star)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "aerial-deployment",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "assert len(n_x_train) == len(y_train)\n",
    "model = RandomForestClassifier(n_estimators=100, \n",
    "                               bootstrap = True,\n",
    "                               max_features = 'sqrt')\n",
    "model.fit(n_x_train, y_train)\n",
    "\n",
    "y_train_star_1 = model.predict(n_x_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "naval-vessel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.9842858454553505"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import log_loss\n",
    "\n",
    "\n",
    "log_loss(y_train, y_train_star_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "moved-creature",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8846434545670643"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_curve\n",
    "\n",
    "accuracy_score(y_train, y_train_star_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "productive-messenger",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[840753,  15173],\n",
       "       [102267,  59868]], dtype=int64)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "confusion_matrix(y_train, y_train_star_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "funded-gregory",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import SGDClassifier\n",
    "\n",
    "assert len(n_x_train) == len(y_train)\n",
    "sdc = SGDClassifier(loss=\"hinge\", penalty=\"l2\", max_iter=100)\n",
    "sdc.fit(n_x_train, y_train)\n",
    "\n",
    "y_train_star_sdc = sdc.predict(n_x_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "third-locking",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.500598206579798"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import log_loss\n",
    "\n",
    "\n",
    "log_loss(y_train, y_train_star_sdc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "pacific-parcel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8407413701143645"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_curve\n",
    "\n",
    "accuracy_score(y_train, y_train_star_sdc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "pointed-booking",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[855926,      0],\n",
       "       [162135,      0]], dtype=int64)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "confusion_matrix(y_train, y_train_star_sdc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "fifth-piece",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_test = clf.predict(n_x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "incomplete-strand",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1]\n",
      "(500578, 2)\n",
      "[[0.05907237 0.94092763]\n",
      " [0.43039941 0.56960059]]\n",
      "(500578,)\n",
      "[0.94092763 0.56960059]\n"
     ]
    }
   ],
   "source": [
    "y_test = clf.predict_proba(n_x_test)\n",
    "print(clf.classes_)\n",
    "print(y_test.shape)\n",
    "print(y_test[3:5, ])\n",
    "\n",
    "y_test = y_test[:, 1]\n",
    "print(y_test.shape)\n",
    "print(y_test[3:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "informed-salon",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
